package com.frame.jfinal.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * @author LRC
 * 配置文件单例对象
 */
public class MyJFinalConfig {
	private Properties pr;
	private static MyJFinalConfig config = new MyJFinalConfig();
	private String fileName = "MyJFinalConfig.properties";
	
	/**
	 * 构造器说明：加载配置文件
	 */
	private MyJFinalConfig(){
		try{
			pr = new Properties();
			pr.load(MyJFinalConfig.class.getClassLoader().getResourceAsStream(fileName));
		}catch(Exception e){
			throw new RuntimeException("加载平台配置文件错误",e);
		}
	}
	
	public static MyJFinalConfig getInstance(){
		return config;
	}
	
	/**
	 * 应用版本:v1.0
	 * 功能说明：根据给定的key查找配置文件中对应的value
	 * @param key
	 * @return
	 */
	public String getValue(String key){
		if(key==null)
			return null;
		String value = pr.getProperty(key);
		return value;
	}
	
	/**
	 * 获取满足name的属性
	 * @param nameLike
	 * @return
	 */
	public String[] getValues(String prefix){
		if(prefix==null||"".equals(prefix))return null;
		Set<Object> keys = this.pr.keySet();
		List<Object> vals = new ArrayList<Object>();
		for(Object k : keys){
			String key = k.toString();
			if(key.startsWith(prefix)){
				vals.add(this.pr.get(key));
			}
		}
		return vals.toArray(new String[]{});
	}
}
